const { v4: uuidv4 } = require("uuid");
const { query, withTransaction } = require("../utils/db");
// 评论相关业务
exports.getComments = async (FDocId) => {
  const sql = `SELECT c.*, u.FUsername 
    FROM t_comments c
    LEFT JOIN t_users u ON c.FUserId = u.FUserId
    WHERE c.FDocId = ?
    ORDER BY c.FCreatedAt ASC`;

  return await query(sql, [FDocId]);
};

exports.addComment = async ({ FDocId, FUserId, FContent }) => {
  const FId = uuidv4();

  await query(
    `INSERT INTO t_comments (FId, FDocId, FUserId, FContent) VALUES (?, ?, ?, ?)`,
    [FId, FDocId, FUserId, FContent]
  );

  return { FId, FContent };
};

exports.deleteComment = async (FId) => {
  const result = await query("DELETE FROM t_comments WHERE FId = ?", [FId]);

  if (result.affectedRows === 0) {
    throw { status: 400, message: "评论不存在" };
  }

  return true;
};